package test.netty.client;

import fox.framework.rpc.client.message.request.RpcClientRequestBuilder;
import fox.framework.rpc.client.message.request.RpcClientRequest;
import io.netty.channel.ChannelHandler;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.SimpleChannelInboundHandler;

/**
 * @author cuichao
 * @Description: ClientChannel
 * @Date: create in 2020/12/21 15:02
 */
@ChannelHandler.Sharable
public class ClientChannel extends SimpleChannelInboundHandler<String> {


    @Override
    public void channelActive(ChannelHandlerContext ctx) throws Exception {
        System.out.println("ClientChannel active");

        Object[] objs = new Object[]{"1",2,"4","test"};

        String protocol = "fox";
        RpcClientRequestBuilder builder = new RpcClientRequestBuilder();
        builder.setPath("/abc/123");
       /* builder.setContentType("kryo");
        builder.setConverter(new KryoConverter());
        builder.setRequestEntityBody(objs);*/
        RpcClientRequest request = builder.build();
        ctx.writeAndFlush(request).addListener(f ->{
            if(f.isSuccess()){
                System.out.println("发送成功");
            }else {
                System.out.println("发送失败");
                f.cause().printStackTrace();
            }
        });
    }

    @Override
    protected void channelRead0(ChannelHandlerContext ctx, String msg) throws Exception {
        System.out.println("接受服务端消息: "+msg);
    }


    @Override
    public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception {
        System.out.println("ServerChannel异常处理:");
        cause.printStackTrace();
    }

}
